<template>
  <BasicModal v-bind="$attrs" @register="register" showFooter :title="getTitle" width="50%" @ok="handleSubmit">

    <BasicForm @register="registerForm">
      <template #Ainput="{ model, field, label }">
        <Ainput mode="single" v-model:value="model[field]" :placeholder="'请选择' + label"
          @success="e => handleSuccess(e, model)">
        </Ainput>
      </template>
    </BasicForm>

  </BasicModal>
</template>
<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicForm, useForm } from '@/components/Form';
import { addIntegralformSchema } from './role.data';
import { BasicModal, useModalInner } from '@/components/Modal';
import { BasicTree, TreeItem } from '@/components/Tree';
import { addYesCardByDateApi } from '@/api/sys/sysData';
import { Form, FormItem, Input, InputNumber, DatePicker, Select, Switch } from 'ant-design-vue';
import Ainput from '@/views/bookManage/components/account/Ainput.vue';

import { useMessage } from '@/hooks/web/useMessage';

const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const treeData = ref<TreeItem[]>([]);
const { createMessage } = useMessage();
const formData = ref({});
const formRef = ref();
const userId = ref('');

const [registerForm, { resetFields, setFieldsValue, updateSchema, validate, clearValidate }] = useForm({
  labelWidth: 100,
  baseColProps: { span: 24 },
  schemas: addIntegralformSchema,
  showActionButtonGroup: false,
});

const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //打开弹层后的回调

  setModalProps({ confirmLoading: false });
  formData.value = {};
  isUpdate.value = !!data?.isUpdate;
  userId.value = '';
  resetFields();
  if (unref(isUpdate)) {
    console.log('---data.record:', data.record);
    userId.value = data.record.userId;
    let json = {
      userId: userId.value,
    };
    formData.value = data.record;
  }
});

const getTitle = computed(() => '补昨日卡');

async function handleSubmit() {
  try {
    const values = await validate();
    setModalProps({ confirmLoading: true });
    //保存
    addYesCardByDateApi(values)
      .then((res) => {
        // console.log("---res::",res)
        if (res.code == 200) {
          createMessage.success(res.message);
        } else {
          createMessage.error(res.message);
        }
      })
      .catch((res) => {
        createMessage.error(res.message);
      })
      .finally(() => {
        closeModal();
        emit('success');
      });

  } finally {
    setModalProps({ confirmLoading: false });
  }
}

function handleSuccess(e, model) {
  model.userId = e.ids;
  model.userName = e.names;
  clearValidate();
}
</script>
